//
//  CYCompanyLoginVC.m
//  CreditAddressBook
//
//  Created by LE on 15/12/15.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYCompanyLoginVC.h"
#import "MBProgressHUD+MJ.h"
#import "CYLoginHttpTool.h"
#import "CYEscape.h"
#import "CYJumpsTool.h"
#import "CYCompanyUserTool.h"

@interface CYCompanyLoginVC ()

@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@end

@implementation CYCompanyLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCancelBtn];
}

/**
 *  设置取消按钮
 */
- (void)setupCancelBtn {
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    btn.tintColor = CYNaviTitleColor;
    self.navigationItem.leftBarButtonItem = btn;
    
    self.loginBtn.layer.cornerRadius = 4;
    self.loginBtn.layer.masksToBounds = YES;
}
/**
 *  取消按钮方法
 */
- (void)cancel {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)logInClick:(id)sender {
    if (self.userNameTF.text.length && self.passwordTF.text.length) {
        [MBProgressHUD showMessage:@"登录中" toView:self.view];
        NSString *phone = self.userNameTF.text;
        NSString *password = [CYEscape encrypt:self.passwordTF.text];
        [CYLoginHttpTool companyLoginWithUserName:phone andPassword:password success:^(NSString *message) {
            [MBProgressHUD hideHUDForView:self.view];
            [MBProgressHUD showSuccess:message];
            [CYJumpsTool jumpToMain:self.view];
        } failure:^(NSString *message, NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
            if (message) {
                [MBProgressHUD showError:message];
            }
        }];
    } else {
        [[[UIAlertView alloc] initWithTitle:@"用户名或登录密码不能为空！" message:nil delegate:nil cancelButtonTitle:CYToolTips otherButtonTitles:nil] show];
    }
    [self.view.window endEditing:YES];
}
@end
